package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityPersonTagType;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.human.dao.IDaoPersonTagType;
import com.ocom.human.service.PersonTagTypeService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PersonTagTypeServiceImpl extends ServiceImpl<IDaoPersonTagType, EntityPersonTagType> implements PersonTagTypeService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public List<EntityPersonTagType> getPerTagGroup(Long comId) {
		QueryWrapper<EntityPersonTagType> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.orderByAsc("group_no");
		return this.list(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public boolean addPerTagGroup(Long comId, EntityPersonTagType entityPersonTagType) {
		QueryWrapper<EntityPersonTagType> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.and(wrappers -> {
			wrappers.eq("group_no", entityPersonTagType.getGroupNo()).or().eq("group_name", entityPersonTagType.getGroupName());
		});
		int count = this.count(queryWrapper);
		if (0 < count) {
			return false;
		}

		entityPersonTagType.setComId(comId);

		return this.save(entityPersonTagType);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public int updatePerTagGroup(Long comId, EntityPersonTagType entityPersonTagType) {
		QueryWrapper<EntityPersonTagType> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("group_name", entityPersonTagType.getGroupName());
		queryWrapper.last("limit 1");
		EntityPersonTagType one = this.getOne(queryWrapper);

		if (null != one && !one.getGroupNo().equals(entityPersonTagType.getGroupNo())) {
			return -2;
		}

		UpdateWrapper<EntityPersonTagType> updateWrapper = Wrappers.update();
		updateWrapper.eq("com_id", comId);
		updateWrapper.eq("group_no", entityPersonTagType.getGroupNo());

		updateWrapper.set("group_name", entityPersonTagType.getGroupName());
		updateWrapper.set("remark", entityPersonTagType.getRemark());
		updateWrapper.set("color", entityPersonTagType.getColor());
		updateWrapper.set("update_time", new Date());

		boolean update = this.update(updateWrapper);

		return update ? 1 : 0;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId")
	public boolean removePerTagGroup(Long comId, Integer groupNo) {
		QueryWrapper<EntityPersonTagType> queryWrapper = Wrappers.query();
		queryWrapper.eq("com_id", comId);
		queryWrapper.eq("group_no", groupNo);

		return this.remove(queryWrapper);
	}
}
